#include <iostream>
#include <unistd.h>
#include "Thread.hpp"
#include "LockGuard.hpp"

using namespace std;

void threadRun(void *args)
{
    string message = static_cast<const char *>(args);
    for (int i = 0; i < 5; i++)
    {
        cout << "我是一个线程," << message << endl;
        sleep(1);
    }
}

int ticket = 1000;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void threadRoutine(void *args)
{
    string message = static_cast<const char *>(args);
    while (true)
    {
        LockGuard lockguard (&mutex);
        if (ticket)
        {
            cout << message << " get a ticket " << ticket-- << endl;
        }
        else
            break;
    }
}

int main()
{
    Thread t1(1, threadRoutine, (void *)"I am first");
    Thread t2(2, threadRoutine, (void *)"I am second");
    Thread t3(3, threadRoutine, (void *)"I am third");
    Thread t4(4, threadRoutine, (void *)"I am fourth");

    t1.run();
    t2.run();
    t3.run();
    t4.run();

    t1.join();
    t2.join();
    t3.join();
    t4.join();

    // Thread t1(1, threadRun, (void *)"hello Alpaca");
    // cout << "thread name: " << t1.name() << " thread id: " << t1.threadid() << " status: " << t1.status() << endl;
    // t1.run();
    // cout << "thread name: " << t1.name() << " thread id: " << t1.threadid() << " status: " << t1.status() << endl;
    // t1.join();
    // cout << "thread name: " << t1.name() << " thread id: " << t1.threadid() << " status: " << t1.status() << endl;
    return 0;
}